package de.lulusoft.anothertorrent.gui.wizards;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.lulusoft.anothertorrent.core.servers.Server;
import de.lulusoft.anothertorrent.core.servers.ServerType;
import de.lulusoft.anothertorrent.gui.exceptions.ExceptionHandler;

public class NewSocketServerWizardPage extends WizardPage {
	
	private Text host = null;
	private Text port = null;

	protected NewSocketServerWizardPage(String pageName) {
		super(pageName);
		setTitle("Create new Server");
		setDescription("Create a new server");
	}

	@Override
	public IWizardPage getNextPage() {
		return null;
	}

	@Override
	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(2, false));
		GridData gd = new GridData();
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;
		GridData gdInput = new GridData();
		gdInput.horizontalAlignment = SWT.FILL;
		gdInput.grabExcessHorizontalSpace = true;
				
		Label lhost = new Label(main, SWT.NONE);
		lhost.setLayoutData(gd);
		lhost.setText("Host:");
		host = new Text(main, SWT.BORDER | SWT.SINGLE);
		host.setLayoutData(gdInput);
		host.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		Label lport = new Label(main, SWT.NONE);
		lport.setLayoutData(gd);
		lport.setText("Port");
		port = new Text(main, SWT.BORDER | SWT.SINGLE);
		port.setLayoutData(gdInput);
		port.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		// Fill existing server info
		if (((NewServerWizard)getWizard()).edit)
		{
			host.setText(((NewServerWizard)getWizard()).server.getHost());
			port.setText(((Integer)((NewServerWizard)getWizard()).server.getPort()).toString());
		}
		
		setControl(main);
	}
	
	@Override
	public boolean isPageComplete() {
		// TODO: Do real checks here..
		if (host != null && host.getText().trim().length() > 0 // make sure host is set
				&& port != null && port.getText().trim().length() > 0 && isInteger(port.getText().trim())// make sure port is set
				)
			return true;
		return false;
	}
	
	private boolean isInteger( String input )  
	{  
		try  
		{  
			Integer.parseInt( input );  
			return true;  
		}
		catch( Exception e)  
		{  
			return false;  
		}
	}

	public Server getServer() {
		Server server = null;
		try {
			server = new Server(ServerType.SOCKET,
					((NewServerWizard)getWizard()).selectServerTypeWizardPage.getServerName(),
					host.getText().trim(),
					Integer.parseInt(port.getText().trim()));
		} catch (Exception e) {
			ExceptionHandler.alert("Could not create Server", e);
		}
		
		return server;
	}

}
